package com.ict.controller;

import com.ict.api.dto.IctUserDTO;
import com.ict.api.result.ApiResponse;
import com.ict.api.vo.IctUserVO;
import com.ict.api.vo.PageResult;
import com.ict.service.IctUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户基本信息表 前端控制器
 * </p>
 *
 * @author wjp
 * @since 2025-03-23
 */
@RestController
@RequestMapping("/ict-user")
public class IctUserController {

    @Autowired
    private IctUserService userService;


    /**
     * 用户注册
     * @param user 用户信息
     * @return ApiResponse
     */
    @PostMapping("/register")
    public ApiResponse register(@RequestBody IctUserDTO user) {
        userService.register(user);
        return ApiResponse.success();
    }

    /**
     * 登录-获取验证码
     * <p>
     * ApiResponse:响应实体对象
     */
    @PostMapping("/loginWithCode")
    public ApiResponse login(@RequestBody IctUserDTO user) {
        //1.获取手机号
        String phone = user.getPhone();

        //2.调用业务层 发送短信验证码
        userService.sendCode(phone);
        return ApiResponse.success();
    }

    /**
     * 用户登录-校验
     */
    @PostMapping("/loginVerification")
    public ApiResponse loginVerification(@RequestBody IctUserDTO user) {
        Map<String, Object> map = userService.loginVerification(user); //{"token":"a.b.c","isNew":true}
        return ApiResponse.success(map);
    }


    /**
     * 查询用户列表（后台管理员）
     * @param user
     * @return
     */
    @GetMapping("/users")
    public ApiResponse<PageResult<IctUserVO>> getUserInfoList(IctUserDTO user) {
        PageResult<IctUserVO> map = userService.getUserInfoList(user);
        return ApiResponse.success(map);
    }

    /**
     * 新增或修改用户（后台管理员）
     * @param user
     * @return
     */
    @PostMapping("/users")
    public ApiResponse addOrUpdateUser(@RequestBody IctUserDTO user) {
        userService.addOrUpdateUser(user);
        return ApiResponse.success();
    }

    /**
     * 删除用户（后台管理员）
     * @param id
     * @return
     */
    @DeleteMapping("/users/{id}")
    public ApiResponse deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return ApiResponse.success();
    }


    /**
     * 根据ID查询用户详情
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    public ApiResponse<IctUserVO> getUserInfo(@PathVariable Long id) {
        IctUserVO map = userService.getUserInfo(id);
        return ApiResponse.success(map);
    }

    /**
     * 启用/禁用用户
     * @param id
     * @return
     */
    @PutMapping("/users/{id}/status")
    public ApiResponse updateUserStatus(@PathVariable Long id, Integer status) {
        userService.updateUserStatus(id, status);
        return ApiResponse.success();
    }
}

